package com.jilefojing.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jilefojing.beans.enums.GenderEnum;
import com.jilefojing.beans.exception.UserContextException;
import com.jilefojing.beans.pojos.WriteBook;
import com.jilefojing.beans.pojos.base.BasePo;
import com.jilefojing.beans.pojos.enhanced.EnhancedDBuddhism;
import com.jilefojing.beans.pojos.enhanced.EnhancedEBookChapter;
import com.jilefojing.beans.pojos.enhanced.EnhancedWriteBook;
import com.jilefojing.beans.pojos.enhanced.base.BaseEnhanced;
import com.jilefojing.beans.query.WriteBookQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.dao.IWriteBookDao;
import com.jilefojing.service.IDBuddhismService;
import com.jilefojing.service.IEBookChapterService;
import com.jilefojing.service.IWriteBookService;
import com.jilefojing.service.base.impl.BaseService;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年10月07日 00时08分14秒
 */
@Service
public class WriteBookService extends BaseService<WriteBook> implements IWriteBookService {
	@Autowired
	private IWriteBookDao writeBookDao;
	@Autowired
	private IEBookChapterService eBookChapterService;
	@Autowired
	private IDBuddhismService dBuddhismService;

	@Override
	public WriteBook saveForActive(WriteBook writeBook, UserContext userContext) throws UserContextException {

		this.checkSave(writeBook);

		WriteBook record = new WriteBook();
		record.setCreateTime(new Date());
		record.setWriteUserName(writeBook.getWriteUserName());
		record.setGender(GenderEnum.getGenderEnumByCode(writeBook.getGender()).getCode());
		record.setAge(writeBook.getAge());
		if (userContext != null) {
			record.setLoginUser(userContext.getUserId());
		}
		record.setAddress(writeBook.getAddress());
		// record.setAllDedicateMerits("回向偈\r\n" +
		// "愿以此功德。庄严佛净土。上报四重恩。下济三途苦。\r\n"
		// + "若有见闻者。悉发菩提心。尽此一报身。同生极乐国。");
		record.setDedicateMerits(writeBook.getDedicateMerits());
		record.setTelephone(writeBook.getTelephone());
		record.setBuddhism(writeBook.getBuddhism());
		record.setBookChapterId(writeBook.getBookChapterId());

		writeBookDao.save(record);
		return record;
	}

	private void checkSave(WriteBook writeBook) throws UserContextException {
		if (!Detect.notEmpty(writeBook.getWriteUserName())) {
			throw new UserContextException("[请填写抄经人名称]");
		}

		if (writeBook.getAge() == null || writeBook.getAge() < 1) {
			throw new UserContextException("[请填写您的年龄]");
		}

		if (!Detect.notEmpty(writeBook.getAddress())) {
			throw new UserContextException("[请填写您的地址]");
		}

		if (!Detect.notEmpty(writeBook.getDedicateMerits())) {
			throw new UserContextException("[请填写回向功德]");
		}

		if (Detect.notEmpty(writeBook.getTelephone()) && !Detect.checkMobileNumber(writeBook.getTelephone())) {
			throw new UserContextException("[请正确填写手机号]");
		}
		if (writeBook.getBuddhism() == null || writeBook.getBuddhism() < 1) {
			throw new UserContextException("[请选择供佛]");
		}
		if (writeBook.getBookChapterId() == null || writeBook.getBookChapterId() < 1) {
			throw new UserContextException("[抄录经书不能为空]");
		}
	}

	@Override
	public Pagination<EnhancedWriteBook> getPagination(WriteBookQuery writeBookQuery, Pagination<EnhancedWriteBook> pagination, UserContext userContext) {
		@SuppressWarnings("unchecked")
		Pagination<WriteBook> writeBookPagination = writeBookDao.getPagination(writeBookQuery, pagination.copy());

		pagination.setCount(writeBookPagination.getCount());
		if (!Detect.notEmpty(writeBookPagination.getItems())) {
			return pagination;
		}
		pagination.setItems(this.convent2Enhanceds(writeBookPagination.getItems()));
		this.appendEnhancedCommons(pagination.getItems(), userContext);
		return pagination;

	}

	@Override
	public List<EnhancedWriteBook> appendEnhancedCommons(List<? extends BaseEnhanced> enhancedItems, UserContext userContext) {
		@SuppressWarnings("unchecked")
		List<EnhancedWriteBook> enhancedWriteBooks = (List<EnhancedWriteBook>) enhancedItems;
		this.appendEnhancedEBookChapter(enhancedWriteBooks, userContext);
		this.appendEnhancedDBuddhism(enhancedWriteBooks, userContext);
		return enhancedWriteBooks;
	}

	@Override
	public List<EnhancedWriteBook> appendEnhancedDBuddhism(List<EnhancedWriteBook> enhancedWriteBooks, UserContext userContext) {
		Set<Long> dBuddhismIds = new HashSet<Long>();
		for (EnhancedWriteBook _EnhancedWriteBook : enhancedWriteBooks) {
			if (_EnhancedWriteBook.getEnhancedDBuddhism() != null) {
				dBuddhismIds.add(_EnhancedWriteBook.getEnhancedDBuddhism().getId());
			}
		}
		if (!Detect.notEmpty(dBuddhismIds)) {
			return enhancedWriteBooks;
		}
		@SuppressWarnings("unchecked")
		List<EnhancedDBuddhism> enhancedDBuddhisms = (List<EnhancedDBuddhism>) dBuddhismService.getEnhanceds(new ArrayList<Long>(dBuddhismIds), userContext);
		if (!Detect.notEmpty(enhancedDBuddhisms)) {
			return enhancedWriteBooks;
		}

		for (EnhancedWriteBook _EnhancedWriteBook : enhancedWriteBooks) {
			if (_EnhancedWriteBook.getEnhancedDBuddhism() != null) {
				for (EnhancedDBuddhism _EnhancedDBuddhism : enhancedDBuddhisms) {
					if (_EnhancedWriteBook.getEnhancedDBuddhism().getId() == _EnhancedDBuddhism.getId()) {
						_EnhancedWriteBook.setEnhancedDBuddhism(_EnhancedDBuddhism);
						break;
					}
				}
			}
		}

		return enhancedWriteBooks;
	}

	@Override
	public List<EnhancedWriteBook> appendEnhancedEBookChapter(List<EnhancedWriteBook> enhancedWriteBooks, UserContext userContext) {
		Set<Long> eBookChapterIds = new HashSet<Long>();
		for (EnhancedWriteBook _EnhancedWriteBook : enhancedWriteBooks) {
			if (_EnhancedWriteBook.getEnhancedEBookChapter() != null) {
				eBookChapterIds.add(_EnhancedWriteBook.getEnhancedEBookChapter().getId());
			}
		}
		if (!Detect.notEmpty(eBookChapterIds)) {
			return enhancedWriteBooks;
		}
		@SuppressWarnings("unchecked")
		List<EnhancedEBookChapter> enhancedEBookChapters = (List<EnhancedEBookChapter>) eBookChapterService.getEnhanceds(new ArrayList<Long>(eBookChapterIds), userContext);
		if (!Detect.notEmpty(enhancedEBookChapters)) {
			return enhancedWriteBooks;
		}

		for (EnhancedWriteBook _EnhancedWriteBook : enhancedWriteBooks) {
			if (_EnhancedWriteBook.getEnhancedEBookChapter() != null) {
				for (EnhancedEBookChapter _EnhancedEBookChapter : enhancedEBookChapters) {
					if (_EnhancedWriteBook.getEnhancedEBookChapter().getId() == _EnhancedEBookChapter.getId()) {
						_EnhancedWriteBook.setEnhancedEBookChapter(_EnhancedEBookChapter);
						break;
					}
				}
			}
		}
		return enhancedWriteBooks;
	}

	@Override
	public List<EnhancedWriteBook> convent2Enhanceds(List<? extends BasePo> pos) {
		@SuppressWarnings("unchecked")
		List<WriteBook> eBookChapters = (List<WriteBook>) pos;
		List<EnhancedWriteBook> enhancedWriteBooks = new ArrayList<EnhancedWriteBook>();
		for (WriteBook _eBookChapter : eBookChapters) {
			enhancedWriteBooks.add(new EnhancedWriteBook(_eBookChapter));
		}

		return enhancedWriteBooks;
	}
}
